<div id="page-body-right" class="panel col-lg-9">
    <div class="panel-heading">
        <h3 class="panel-title">主机列表</h3>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table id="host_table" class="table table-striped">
                <thead>
                <tr>
                    <th>Hostname</th>
                    <th>IP</th>
                    <th>IDC</th>
                    <th>Port</th>
                    <th>Username</th>
                    <th>Login</th>
                    <th>Token</th>
                </tr>
                </thead>
                <tbody id="hostlist">
                </tbody>
            </table>
        </div>


    </div>

</div>
<div id="page-body">
    <div id="disconnect" style="margin-top: 20px">
            <button type="button" class="btn btn-danger">关闭连接</button>
    </div>
    <div id="term" class="son"></div>
</div>
<!--<script src="/statics/js/luffy.js"></script>-->
<script src="/static/xterm.js"></script>
<link href="/static/xterm.css" rel="stylesheet" type="text/css"/>
<script>

function GetHostlist(gid, self) {
    $.get("{% url 'get_host_list' %}", {'gid': gid}, function (callback) {
        var data = JSON.parse(callback);
        console.log(data);

        var trs = '';
        $.each(data, function (index, i) {
            {#                    var trs = "<tr><td>" + i.host__hostname + "</td><td>" + i.host__ip__addr#}
            trs += "<tr><td>";
            trs += i.host__hostname + "</td><td>";
            trs += i.host__ip_addr + "</td><td>";
            trs += i.host__idc__name + "</td><td>";
            trs += i.host__port + "</td><td>";
            trs += i.host_user__username + "</td><td>";
            trs += "<a class='btn btn-info' onclick=GetToken(this,'" + i.id + "')>Token</a>";
            trs += "<span >" + i.id + "</span><button id='conn_button' onclick=Getid(this) type='button' class='btn btn-info' >连接</button>";
            trs += "</td></tr>"
        });
        $("#hostlist").html(trs);
    });
    $(self).addClass("active").siblings().removeClass("active")

}



function openTerminal(options) {
            var client = new IronSSHClient();
            copytext = false;


            var term = new Terminal(
                    {
                        cols: 80,
                        rows: 24,
                        handler: function (key) {
                            console.log(key);
                            if (copytext){
                                client.send(copytext);
                                copytext = false

                            }
                            client.send(key);



                        },
                        screenKeys: true,
                        useStyle: true,
                        cursorBlink: true
                    });
            term.open();
            $('.terminal').detach().appendTo('#term');
            term.resize(80,24)
            term.write("开始连接....");


            client.connect(
                    $.extend(options, {
                                onError: function (error) {
                                    term.write("错误：" + error + "\r\n");
                                },
                                onConnect: function () {
                                    term.write('\r');
                                },
                                onClose: function () {
                                    term.write("对方断开了连接......");
                                }, //term.destroy()
                                onData: function (data) {

                                    if (copytext){
                                        term.write(copytext);
                                        copytext = false
                                    }
                                    else {
                                        term.write(data)
                                    }
                                }
                            }
                    )
            );


        }

    function IronSSHClient(){

}

IronSSHClient.prototype._generateURl = function (options) {
    if (window.location.protocol == "https") {
        var protocol = "wss://";
    }else {
        var protocol = "ws://";
    }

    var url = protocol + window.location.host + "/host/" + encodeURIComponent(options.des_id) + "/";
    return url;
};


IronSSHClient.prototype.connect = function (options) {
    var des_url = this._generateURl(options);

    if (window.WebSocket){
        this._connection = new WebSocket(des_url);
    }
    else if (window.MozWebSocket){
        this._connection = new MozWebSocket(des_url);
    }
    else {
        options.onError("当前浏览器不支持WebSocket");
        return;
    }
    this._connection.onopen = function () {
        options.onConnect();
    };

    this._connection.onmessage = function (evt) {
        var data = JSON.parse(evt.data.toString());
        console.log(evt,data);
        if (data.error !== undefined){
            options.onError(data.error);
        }
        else {
            options.onData(data.data);
        }

    };


    this._connection.onclose = function (evt) {
        options.onClose();
    };
};


IronSSHClient.prototype.send = function (data) {

    this._connection.send(JSON.stringify({'data':data}));

};

</script>
